﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RealEstate.Models;
using RealEstate.Common;
using Telerik.Web.UI;

namespace RealEstate
{
    public partial class EditLoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = 0;
                if (!Roles.IsUserInRole("admin"))
                {
                    Response.Redirect("ByLoanStatus.aspx");
                }

                if (Request.Params["id"] != null && int.TryParse(Request.Params["id"].ToString(), out id))
                {
                    Utilities.LoadClientForDropDownlist(ddlClient);
                    Utilities.LoadClientForDropDownlist(ddlCoClient);
                    Utilities.LoadPropertyForDropDownlist(ddlSubject);
                    Utilities.LoadPropertyForDropDownlist(ddlMailing);
                    Utilities.LoadStateForDropDownlist(ddlState1);
                    Utilities.LoadStateForDropDownlist(ddlState2);
                    Utilities.LoadCityForDropDownlist(ddlCity1);
                    Utilities.LoadCityForDropDownlist(ddlCity2);
                    Utilities.LoadPropertyTypeForDropDownlist(ddlSubjectPropertyType);
                    Utilities.LoadStatusForDropDownlist(ddlCaseStatus);
                    LoadData(id);
                    hplHistory.Attributes.Add("onclick", string.Concat("openWin('/views/transaction/StatusHistory.aspx?id=", id, "'); return false;"));
                }
            }
        }

        private void LoadData(int id)
        {
            TransactionEntities entity = new TransactionEntities();
            TRANSACTION item = entity.GetById(id);
            if (item != null)
            {
                //Case Information
                if (item.DateOpen != null)
                    rdpDateOpen.SelectedDate = item.DateOpen.Value;
                if (item.CaseIDNumber != null)
                    txtCaseIDNumber.Text = item.CaseIDNumber;

                //Loan Broker Info
                txtLoanBrokerName.Text = item.LoanBrokerName;
                txtLoanCompany.Text = item.LoanCompany;

                //ClientInformation

                if (item.Client != null)
                {
                    txtClientLastName.Text = item.Client.LastName;
                    txtClientFirstName.Text = item.Client.FirstName;
                    txtClientSSN.Text = item.Client.SSN;
                    txtClientWorkPhone.Text = item.Client.WorkPhNumber;
                    txtClientFaxNumber.Text = item.Client.FaxNumber;
                    txtClientHomePhone.Text = item.Client.HomePhNumber;
                    txtClientCellPhone.Text = item.Client.CellNumber;
                    txtClientEmail.Text = item.Client.Email;
                    if (item.Client.BirthDate != null)
                        rdpClientBirthDate.SelectedDate = item.Client.BirthDate.Value;

                    ddlClient.SelectedIndex = Utilities.GetIndexByValue(ddlClient, item.Client.ClientId.ToString());
                }

                if (item.Client1 != null)
                {
                    txtCoClientLastName.Text = item.Client1.LastName;
                    txtCoClientFirstName.Text = item.Client1.FirstName;
                    txtCoClientSSN.Text = item.Client1.SSN;
                    txtCoClientWorkPhone.Text = item.Client1.WorkPhNumber;
                    txtCoClientFaxNumber.Text = item.Client1.FaxNumber;
                    txtCoClientHomePhone.Text = item.Client1.HomePhNumber;
                    txtCoClientCellPhone.Text = item.Client1.CellNumber;
                    txtCoClientEmail.Text = item.Client1.Email;
                    if (item.Client1.BirthDate != null)
                        rdpCoClientBirthDate.SelectedDate = item.Client1.BirthDate.Value;

                    ddlCoClient.SelectedIndex = Utilities.GetIndexByValue(ddlCoClient, item.Client1.ClientId.ToString());
                }

                //Property Information                           
                if (item.Property1 != null)
                {
                    ddlSubjectPropertyType.SelectedIndex = Utilities.GetIndexByValue(ddlSubjectPropertyType, item.Property1.PropertyTypeId.ToString());
                    txtSubjectStreetNumber.Text = item.Property1.StreetNumer;
                    txtSubjectStreetName.Text = item.Property1.StreetName;
                    ddlState1.SelectedIndex = Utilities.GetIndexByValue(ddlState1, item.Property1.StateId.ToString());
                    ddlCity1.SelectedIndex = Utilities.GetIndexByValue(ddlState1, item.Property1.CityId.ToString());
                    txtZipCode.Text = item.Property1.ZipCode;

                    ddlSubject.SelectedIndex = Utilities.GetIndexByValue(ddlSubject, item.Property1.PropertyId.ToString());
                }

                if (item.Property2 != null)
                {
                    txtMailStreetNumber.Text = item.Property2.StreetNumer;
                    txtMailStreetName.Text = item.Property2.StreetName;
                    ddlState2.SelectedIndex = Utilities.GetIndexByValue(ddlState2, item.Property2.StateId.ToString());
                    ddlCity2.SelectedIndex = Utilities.GetIndexByValue(ddlState2, item.Property2.CityId.ToString());
                    txtZipCode_New.Text = item.Property2.ZipCode;

                    ddlMailing.SelectedIndex = Utilities.GetIndexByValue(ddlMailing, item.Property2.PropertyId.ToString());
                }

                //Transaction Details
                if (item.PurchaseSoldPrice != null)
                    txtPurchaseSoldPrice.Text = item.PurchaseSoldPrice.ToString();
                if (item.TotalEquity != null)
                    txtTotalEquity.Text = item.TotalEquity.ToString();
                txtProgram.Text = item.Program;
                txtPreviousProgram.Text = item.PreviousProgram;
                if (item.DateOfCOE != null)
                    rdpDateOfCOE.SelectedDate = item.DateOfCOE.Value;
                if (item.LoanAmount != null)
                    txtLoanAmount.Text = item.LoanAmount.ToString();

                if (item.DateLocked != null)
                    rdpDateLocked.SelectedDate = item.DateLocked.Value;

                txtPreviousCOE.Text = item.PreviousCOE;
                txtTargetProgram.Text = item.TargetProgram;

                //Financial Info
                if (item.HouseHoldGrossIncome != null)
                    txtHouseHoldIncome.Text = item.HouseHoldGrossIncome.ToString();
                if (item.HouseHoldDebits != null)
                    txtHouseHoldDebits.Text = item.HouseHoldDebits.ToString();

                //TransactionStatus_Financing
                ddlCaseStatus.SelectedIndex = Utilities.GetIndexByValue(ddlCaseStatus, item.StatusId.ToString());
                if (item.DateOfStatus != null)
                    rdpDateStatus.SelectedDate = item.DateOfStatus.Value;

                //My Income Info
                if (item.TotalGrossIncome != null)
                    txtTotalGrossIncome.Text = item.TotalGrossIncome.ToString();

                if (item.CreditToClient != null)
                    txtCreditToClient.Text = item.CreditToClient.ToString();
                txtWayToCredit.Text = item.WayToCredit;
                if (item.Expense != null)
                    txtExpense.Text = item.Expense.ToString();
                txtExpenseType.Text = item.ExpenseType;
                txtAssistantName.Text = item.AssisistantName;

                if (item.AssistantFee != null)
                    txtPayToAssistant.Text = item.AssistantFee.ToString();

                if (item.ReferalFee != null)
                    txtReferalFee.Text = item.ReferalFee.ToString();

                if (item.GrossCommission != null)
                    txtGrossCommission.Text = item.GrossCommission.ToString();

                if (item.MyGrossIncome != null)
                    txtMyGrossIncome.Text = item.MyGrossIncome.ToString();

                if (item.OtherIncome != null)
                    txtOtherIncome.Text = item.OtherIncome.ToString();

                if (item.TotalGrossIncome != null)
                    txtTotalGrossIncome.Text = item.TotalGrossIncome.ToString();

                if (item.TransactionFee != null)
                    txtTransactionFee.Text = item.TransactionFee.ToString();

                txtReasonForOtherIncome.Text = item.ReasonForOtherIncome;

                //Notes
                txtNotes.Text = item.Notes;
            }
        }

        protected void imgEdit_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                int id = 0;
                if (Request.Params["id"] != null && int.TryParse(Request.Params["id"].ToString(), out id))
                {
                    ClientEntities clientEntity = new ClientEntities();
                    PropertyEntities propertyEntity = new PropertyEntities();

                    TransactionEntities entity = new TransactionEntities();
                    TRANSACTION item = entity.GetById(id);

                    //Case Information
                    if (rdpDateOpen.SelectedDate != null)
                        item.DateOpen = rdpDateOpen.SelectedDate;
                    else
                        item.DateOpen = DateTime.Now;

                    item.CaseIDNumber = txtCaseIDNumber.Text;

                    //Loan Broker Info
                    item.LoanBrokerName = txtLoanBrokerName.Text;
                    item.LoanCompany = txtLoanCompany.Text;

                    //ClientInformation
                    if (ddlClient.Text != string.Empty)
                    {
                        Client client = clientEntity.GetById(int.Parse(ddlClient.SelectedItem.Value));
                        client.LastName = txtClientLastName.Text;
                        client.FirstName = txtClientFirstName.Text;
                        client.SSN = txtClientSSN.Text;
                        client.WorkPhNumber = txtClientWorkPhone.Text;
                        client.FaxNumber = txtClientFaxNumber.Text;
                        client.HomePhNumber = txtClientHomePhone.Text;
                        client.CellNumber = txtClientCellPhone.Text;
                        client.Email = txtClientEmail.Text;
                        if (rdpClientBirthDate.SelectedDate != null)
                            client.BirthDate = rdpClientBirthDate.SelectedDate;

                        clientEntity.Update(client);

                        item.ClientId = client.ClientId;
                    }
                    else
                    {
                        if (item.Client != null)
                        {
                            item.Client.LastName = txtClientLastName.Text;
                            item.Client.FirstName = txtClientFirstName.Text;
                            item.Client.SSN = txtClientSSN.Text;
                            item.Client.WorkPhNumber = txtClientWorkPhone.Text;
                            item.Client.FaxNumber = txtClientFaxNumber.Text;
                            item.Client.HomePhNumber = txtClientHomePhone.Text;
                            item.Client.CellNumber = txtClientCellPhone.Text;
                            item.Client.Email = txtClientEmail.Text;

                            if (rdpClientBirthDate.SelectedDate != null)
                                item.Client.BirthDate = rdpClientBirthDate.SelectedDate;
                            else
                                item.Client.BirthDate = DateTime.Now;
                        }
                        else if (!string.IsNullOrEmpty(txtClientLastName.Text) || !string.IsNullOrEmpty(txtClientFirstName.Text))
                        {
                            Client client = new Client();
                            client.LastName = txtClientLastName.Text;
                            client.FirstName = txtClientFirstName.Text;
                            client.SSN = txtClientSSN.Text;
                            client.WorkPhNumber = txtClientWorkPhone.Text;
                            client.FaxNumber = txtClientFaxNumber.Text;
                            client.HomePhNumber = txtClientHomePhone.Text;
                            client.CellNumber = txtClientCellPhone.Text;
                            client.Email = txtClientEmail.Text;

                            if (rdpClientBirthDate.SelectedDate != null)
                                client.BirthDate = rdpClientBirthDate.SelectedDate;
                            else
                                client.BirthDate = DateTime.Now;

                            clientEntity.Add(client);

                            if (client.ClientId > 0)
                                item.ClientId = client.ClientId;
                        }
                    }

                    if (ddlCoClient.Text != string.Empty)
                    {
                        Client coClient = clientEntity.GetById(int.Parse(ddlCoClient.SelectedItem.Value));
                        coClient.LastName = txtCoClientLastName.Text;
                        coClient.FirstName = txtCoClientFirstName.Text;
                        coClient.SSN = txtCoClientSSN.Text;
                        coClient.WorkPhNumber = txtCoClientWorkPhone.Text;
                        coClient.FaxNumber = txtCoClientFaxNumber.Text;
                        coClient.HomePhNumber = txtCoClientHomePhone.Text;
                        coClient.CellNumber = txtCoClientCellPhone.Text;
                        coClient.Email = txtCoClientEmail.Text;
                        if (rdpClientBirthDate.SelectedDate != null)
                            coClient.BirthDate = rdpCoClientBirthDate.SelectedDate;

                        clientEntity.Update(coClient);

                        item.CoClientId = coClient.ClientId;
                    }
                    else
                    {
                        if (item.Client1 != null)
                        {
                            item.Client1.LastName = txtCoClientLastName.Text;
                            item.Client1.FirstName = txtCoClientFirstName.Text;
                            item.Client1.SSN = txtCoClientSSN.Text;
                            item.Client1.WorkPhNumber = txtCoClientWorkPhone.Text;
                            item.Client1.FaxNumber = txtCoClientFaxNumber.Text;
                            item.Client1.HomePhNumber = txtCoClientHomePhone.Text;
                            item.Client1.CellNumber = txtCoClientCellPhone.Text;
                            item.Client1.Email = txtCoClientEmail.Text;

                            if (rdpCoClientBirthDate.SelectedDate != null)
                                item.Client1.BirthDate = rdpCoClientBirthDate.SelectedDate;
                            else
                                item.Client1.BirthDate = DateTime.Now;
                        }
                        else if (!string.IsNullOrEmpty(txtCoClientLastName.Text) || !string.IsNullOrEmpty(txtCoClientFirstName.Text))
                        {
                            Client coClient = new Client();
                            coClient.LastName = txtCoClientLastName.Text;
                            coClient.FirstName = txtCoClientFirstName.Text;
                            coClient.SSN = txtCoClientSSN.Text;
                            coClient.WorkPhNumber = txtCoClientWorkPhone.Text;
                            coClient.FaxNumber = txtCoClientFaxNumber.Text;
                            coClient.HomePhNumber = txtCoClientHomePhone.Text;
                            coClient.CellNumber = txtCoClientCellPhone.Text;
                            coClient.Email = txtCoClientEmail.Text;

                            if (rdpCoClientBirthDate.SelectedDate != null)
                                coClient.BirthDate = rdpCoClientBirthDate.SelectedDate;
                            else
                                coClient.BirthDate = DateTime.Now;

                            //Add CoClient
                            clientEntity.Add(coClient);
                            if (coClient.ClientId > 0)
                                item.CoClientId = coClient.ClientId;
                        }
                    }

                    //Property Information           
                    if (ddlSubject.Text != string.Empty)
                    {
                        Property subject = propertyEntity.GetById(int.Parse(ddlSubject.SelectedItem.Value));
                        if (!string.IsNullOrEmpty(ddlSubjectPropertyType.Text))
                            subject.PropertyTypeId = int.Parse(ddlSubjectPropertyType.SelectedItem.Value);
                        subject.StreetNumer = txtSubjectStreetNumber.Text;
                        subject.StreetName = txtSubjectStreetName.Text;
                        if (!string.IsNullOrEmpty(ddlState1.Text))
                            subject.StateId = int.Parse(ddlState1.SelectedItem.Value);
                        if (!string.IsNullOrEmpty(ddlCity1.Text))
                            subject.CityId = int.Parse(ddlCity1.SelectedItem.Value);
                        subject.ZipCode = txtZipCode.Text;

                        //Update SubjectAddress
                        propertyEntity.Update(subject);
                        item.SubjectAddressId = subject.PropertyId;
                    }
                    else
                    {
                        if (item.Property1 != null)
                        {
                            if (!string.IsNullOrEmpty(ddlSubjectPropertyType.Text))
                                item.Property1.PropertyTypeId = int.Parse(ddlSubjectPropertyType.SelectedItem.Value);
                            item.Property1.StreetNumer = txtSubjectStreetNumber.Text;
                            item.Property1.StreetName = txtSubjectStreetName.Text;
                            if (!string.IsNullOrEmpty(ddlState1.Text))
                                item.Property1.StateId = int.Parse(ddlState1.SelectedItem.Value);
                            if (!string.IsNullOrEmpty(ddlCity1.Text))
                                item.Property1.CityId = int.Parse(ddlCity1.SelectedItem.Value);
                            item.Property1.ZipCode = txtZipCode.Text;
                        }
                        else if (!string.IsNullOrEmpty(txtSubjectStreetNumber.Text) || !string.IsNullOrEmpty(txtSubjectStreetName.Text))
                        {
                            Property subject = new Property();
                            if (!string.IsNullOrEmpty(ddlSubjectPropertyType.Text))
                                subject.PropertyTypeId = int.Parse(ddlSubjectPropertyType.SelectedItem.Value);
                            subject.StreetNumer = txtSubjectStreetNumber.Text;
                            subject.StreetName = txtSubjectStreetName.Text;
                            if (!string.IsNullOrEmpty(ddlState1.Text))
                                subject.StateId = int.Parse(ddlState1.SelectedItem.Value);
                            if (!string.IsNullOrEmpty(ddlCity1.Text))
                                subject.CityId = int.Parse(ddlCity1.SelectedItem.Value);
                            subject.ZipCode = txtZipCode.Text;
                            //Add SubjectAddress
                            propertyEntity.Add(subject);

                            if (subject.PropertyId > 0)
                                item.SubjectAddressId = subject.PropertyId;
                        }
                    }

                    //Add mailing
                    if (ddlMailing.Text != string.Empty)
                    {
                        Property mail = propertyEntity.GetById(int.Parse(ddlMailing.SelectedItem.Value));
                        mail.StreetNumer = txtMailStreetNumber.Text;
                        mail.StreetName = txtMailStreetName.Text;
                        if (!string.IsNullOrEmpty(ddlState2.Text))
                            mail.StateId = int.Parse(ddlState2.SelectedItem.Value);
                        if (!string.IsNullOrEmpty(ddlCity2.Text))
                            mail.CityId = int.Parse(ddlCity2.SelectedItem.Value);
                        mail.ZipCode = txtZipCode_New.Text;
                        //Update MaillingAddress
                        propertyEntity.Update(mail);
                        item.MailingAddressId = mail.PropertyId;
                    }
                    else
                    {
                        if (item.Property2 != null)
                        {
                            item.Property2.PropertyTypeId = int.Parse(ddlMailing.SelectedItem.Value);
                            item.Property2.StreetNumer = txtSubjectStreetNumber.Text;
                            item.Property2.StreetName = txtSubjectStreetName.Text;
                            if (!string.IsNullOrEmpty(ddlState2.Text))
                                item.Property2.StateId = int.Parse(ddlState2.SelectedItem.Value);
                            if (!string.IsNullOrEmpty(ddlCity2.Text))
                                item.Property2.CityId = int.Parse(ddlCity2.SelectedItem.Value);
                            item.Property2.ZipCode = txtZipCode_New.Text;
                        }
                        else if (!string.IsNullOrEmpty(txtMailStreetNumber.Text) || !string.IsNullOrEmpty(txtMailStreetName.Text))
                        {

                            Property mail = new Property();
                            mail.StreetNumer = txtMailStreetNumber.Text;
                            mail.StreetName = txtMailStreetName.Text;
                            if (!string.IsNullOrEmpty(ddlState2.Text))
                                mail.StateId = int.Parse(ddlState2.SelectedItem.Value);
                            if (!string.IsNullOrEmpty(ddlCity2.Text))
                                mail.CityId = int.Parse(ddlCity2.SelectedItem.Value);
                            mail.ZipCode = txtZipCode_New.Text;
                            //Add MaillingAddress
                            propertyEntity.Add(mail);
                            if (mail.PropertyId > 0)
                                item.MailingAddressId = mail.PropertyId;
                        }
                    }

                    //Transaction Details
                    if (!string.IsNullOrEmpty(txtPurchaseSoldPrice.Text))
                        item.PurchaseSoldPrice = decimal.Parse(txtPurchaseSoldPrice.Text);
                    if (!string.IsNullOrEmpty(txtTotalEquity.Text))
                        item.TotalEquity = decimal.Parse(txtTotalEquity.Text);
                    item.Program = txtProgram.Text;
                    item.PreviousProgram = txtPreviousProgram.Text;
                    if (rdpDateOfCOE.SelectedDate != null)
                        item.DateOfCOE = rdpDateOfCOE.SelectedDate;

                    if (!string.IsNullOrEmpty(txtLoanAmount.Text))
                        item.LoanAmount = int.Parse(txtLoanAmount.Text);
                    if (rdpDateLocked.SelectedDate != null)
                        item.DateLocked = rdpDateLocked.SelectedDate;
                    item.PreviousCOE = txtPreviousCOE.Text;
                    item.TargetProgram = txtTargetProgram.Text;

                    //Financial Info
                    if (!string.IsNullOrEmpty(txtHouseHoldIncome.Text))
                        item.HouseHoldGrossIncome = decimal.Parse(txtHouseHoldIncome.Text);
                    if (!string.IsNullOrEmpty(txtHouseHoldDebits.Text))
                        item.HouseHoldDebits = decimal.Parse(txtHouseHoldDebits.Text);

                    //TransactionStatus_Financing               
                    if (!string.IsNullOrEmpty(ddlCaseStatus.Text))
                    {
                        //Add Transaction Status History                                       
                        if (item.StatusId != int.Parse(ddlCaseStatus.SelectedItem.Value) || item.Status == null)
                        {
                            TransactionStatusEntities statusEntity = new TransactionStatusEntities();
                            TransactionStatus status = new TransactionStatus();
                            status.StatusId = int.Parse(ddlCaseStatus.SelectedItem.Value);
                            status.Date = DateTime.Now;
                            status.TransactionId = item.TransactionId;
                            statusEntity.Add(status);

                            item.StatusId = int.Parse(ddlCaseStatus.SelectedItem.Value);
                        }
                    }

                    if (rdpDateStatus.SelectedDate != null)
                        item.DateOfStatus = rdpDateStatus.SelectedDate;

                    //My Income Info
                    if (!string.IsNullOrEmpty(txtTotalGrossIncome.Text))
                        item.TotalGrossIncome = decimal.Parse(txtTotalGrossIncome.Text);
                    if (!string.IsNullOrEmpty(txtCreditToClient.Text))
                        item.CreditToClient = decimal.Parse(txtCreditToClient.Text);
                    item.WayToCredit = txtWayToCredit.Text;
                    if (!string.IsNullOrEmpty(txtExpense.Text))
                        item.Expense = decimal.Parse(txtExpense.Text);
                    item.ExpenseType = txtExpenseType.Text;
                    item.AssisistantName = txtAssistantName.Text;

                    if (!string.IsNullOrEmpty(txtPayToAssistant.Text))
                        item.AssistantFee = decimal.Parse(txtPayToAssistant.Text);

                    if (!string.IsNullOrEmpty(txtReferalFee.Text))
                        item.ReferalFee = decimal.Parse(txtReferalFee.Text);

                    if (!string.IsNullOrEmpty(txtGrossCommission.Text))
                        item.GrossCommission = decimal.Parse(txtGrossCommission.Text);

                    if (!string.IsNullOrEmpty(txtMyGrossIncome.Text))
                        item.MyGrossIncome = decimal.Parse(txtMyGrossIncome.Text);

                    if (!string.IsNullOrEmpty(txtOtherIncome.Text))
                        item.OtherIncome = decimal.Parse(txtOtherIncome.Text);

                    if (!string.IsNullOrEmpty(txtTransactionFee.Text))
                        item.TransactionFee = decimal.Parse(txtTransactionFee.Text);

                    item.ReasonForOtherIncome = txtReasonForOtherIncome.Text;

                    //Notes
                    item.Notes = txtNotes.Text;
                    entity.Update(item);

                    // back to detail
                    Response.Redirect(string.Concat("LoanDetails.aspx?id=", id));
                }
            }
            catch (Exception ex)
            { }
        }

        protected void imgBack_Click(object sender, ImageClickEventArgs e)
        {
            int id = 0;
            if (Request.Params["viewid"] != null && int.TryParse(Request.Params["viewid"].ToString(), out id))
            {
                Response.Redirect(Utilities.GetViewName(id));
            }
            else
            {
                Response.Redirect(Utilities.GetViewName(int.Parse(Constants.ViewByLoanStatusId)));
            }
        }

        protected void ddlClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlClient.Text != string.Empty)
            {
                LoadClient(int.Parse(ddlClient.SelectedItem.Value));
            }
            else
            {
                LoadClient(0);
            }
        }

        protected void ddlCoClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCoClient.Text != string.Empty)
            {
                LoadCoClient(int.Parse(ddlCoClient.SelectedItem.Value));
            }
            else
            {
                LoadCoClient(0);
            }
        }

        private void LoadClient(int id)
        {
            ClientEntities entity = new ClientEntities();
            Client client = entity.GetById(id);
            if (client != null)
            {
                txtClientCellPhone.Text = client.CellNumber;
                txtClientEmail.Text = client.Email;
                txtClientFaxNumber.Text = client.FaxNumber;
                txtClientFirstName.Text = client.FirstName;
                txtClientHomePhone.Text = client.HomePhNumber;
                txtClientLastName.Text = client.LastName;
                txtClientSSN.Text = client.SSN;
                txtClientWorkPhone.Text = client.WorkPhNumber;
                if (client.BirthDate != null)
                    rdpClientBirthDate.SelectedDate = client.BirthDate;
            }
            else
            {
                txtClientCellPhone.Text = string.Empty;
                txtClientEmail.Text = string.Empty;
                txtClientFaxNumber.Text = string.Empty;
                txtClientFirstName.Text = string.Empty;
                txtClientHomePhone.Text = string.Empty;
                txtClientLastName.Text = string.Empty;
                txtClientSSN.Text = string.Empty;
                txtClientWorkPhone.Text = string.Empty;
                rdpClientBirthDate.SelectedDate = null;
            }
        }

        private void LoadCoClient(int id)
        {
            ClientEntities entity = new ClientEntities();
            Client client = entity.GetById(id);
            if (client != null)
            {
                txtCoClientCellPhone.Text = client.CellNumber;
                txtCoClientEmail.Text = client.Email;
                txtCoClientFaxNumber.Text = client.FaxNumber;
                txtCoClientFirstName.Text = client.FirstName;
                txtCoClientHomePhone.Text = client.HomePhNumber;
                txtCoClientLastName.Text = client.LastName;
                txtCoClientSSN.Text = client.SSN;
                txtCoClientWorkPhone.Text = client.WorkPhNumber;
                if (client.BirthDate != null)
                    rdpCoClientBirthDate.SelectedDate = client.BirthDate;
            }
            else
            {
                txtCoClientCellPhone.Text = string.Empty;
                txtCoClientEmail.Text = string.Empty;
                txtCoClientFaxNumber.Text = string.Empty;
                txtCoClientFirstName.Text = string.Empty;
                txtCoClientHomePhone.Text = string.Empty;
                txtCoClientLastName.Text = string.Empty;
                txtCoClientSSN.Text = string.Empty;
                txtCoClientWorkPhone.Text = string.Empty;
                rdpCoClientBirthDate.SelectedDate = null;
            }
        }

        protected void ddlSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSubject.Text != string.Empty)
            {
                LoadSubject(int.Parse(ddlSubject.SelectedItem.Value));
            }
            else
            {
                LoadSubject(0);
            }
        }

        protected void ddlMailing_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlMailing.Text != string.Empty)
            {
                LoadMailing(int.Parse(ddlMailing.SelectedItem.Value));
            }
            else
            {
                LoadMailing(0);
            }
        }

        private void LoadSubject(int id)
        {
            PropertyEntities entity = new PropertyEntities();
            Property subject = entity.GetById(id);
            if (subject != null)
            {
                ddlSubjectPropertyType.SelectedIndex = Utilities.GetIndexByText(ddlSubjectPropertyType, subject.PropertyTypeId.ToString());
                txtSubjectStreetNumber.Text = subject.StreetNumer;
                txtSubjectStreetName.Text = subject.StreetName;
                ddlCity1.SelectedIndex = Utilities.GetIndexByValue(ddlCity1, subject.CityId.ToString());
                txtZipCode.Text = subject.ZipCode;
            }
            else
            {
                txtSubjectStreetNumber.Text = string.Empty;
                txtSubjectStreetName.Text = string.Empty;
                ddlCity1.SelectedIndex = 0;
                txtZipCode.Text = string.Empty;
            }
        }

        private void LoadMailing(int id)
        {
            PropertyEntities entity = new PropertyEntities();
            Property mail = entity.GetById(id);
            if (mail != null)
            {
                txtMailStreetNumber.Text = mail.StreetNumer;
                txtMailStreetName.Text = mail.StreetName;
                ddlCity2.SelectedIndex = Utilities.GetIndexByValue(ddlCity2, mail.CityId.ToString());
                txtZipCode_New.Text = mail.ZipCode;
            }
            else
            {
                txtMailStreetNumber.Text = string.Empty;
                txtMailStreetName.Text = string.Empty;
                ddlCity2.SelectedIndex = 0;
                txtZipCode_New.Text = string.Empty;
            }
        }

        protected void ddlState1_SelectedIndexChanged(object sender, EventArgs e)
        {
            StateEntities entity = new StateEntities();
            if (ddlState1.Text != string.Empty)
            {
                LoadCity1();
            }
            else
                Utilities.LoadCityForDropDownlist(ddlCity1);

        }

        protected void ddlState2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlState2.Text != string.Empty)
            {
                LoadCity2();
            }
            else
                Utilities.LoadCityForDropDownlist(ddlCity2);
        }

        private void LoadCity1()
        {
            StateEntities entity = new StateEntities();
            ddlCity1.DataSource = entity.GetCityById(int.Parse(ddlState1.SelectedItem.Value));
            ddlCity1.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddlCity1.Items.Insert(0, item);
        }

        private void LoadCity2()
        {
            StateEntities entity = new StateEntities();
            ddlCity2.DataSource = entity.GetCityById(int.Parse(ddlState2.SelectedItem.Value));
            ddlCity2.DataBind();
            RadComboBoxItem item = new RadComboBoxItem(string.Empty);
            ddlCity2.Items.Insert(0, item);
        }

        protected void ddlCity1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CityEntities entity = new CityEntities();
            if (ddlCity1.Text != string.Empty)
                ddlState1.SelectedIndex = Utilities.GetIndexByValue(ddlState1, entity.GetById(int.Parse(ddlCity1.SelectedItem.Value)).StateId.ToString());
        }

        protected void ddlCity2_SelectedIndexChanged(object sender, EventArgs e)
        {
            CityEntities entity = new CityEntities();
            if (ddlCity2.Text != string.Empty)
                ddlState2.SelectedIndex = Utilities.GetIndexByValue(ddlState2, entity.GetById(int.Parse(ddlCity2.SelectedItem.Value)).StateId.ToString());
        }

        protected void chkSame_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSame.Checked)
            {
                if (!string.IsNullOrEmpty(ddlSubject.Text))
                    ddlMailing.SelectedIndex = Utilities.GetIndexByValue(ddlSubject, ddlSubject.SelectedItem.Value);
                if (!string.IsNullOrEmpty(txtSubjectStreetName.Text))
                    txtMailStreetName.Text = txtSubjectStreetName.Text;
                if (!string.IsNullOrEmpty(txtSubjectStreetNumber.Text))
                    txtMailStreetNumber.Text = txtSubjectStreetNumber.Text;
                if (!string.IsNullOrEmpty(ddlState1.Text))
                {
                    ddlState2.SelectedIndex = Utilities.GetIndexByValue(ddlState1, ddlState1.SelectedItem.Value);
                    LoadCity2();
                }

                if (!string.IsNullOrEmpty(ddlCity1.Text))
                    ddlCity2.SelectedIndex = Utilities.GetIndexByValue(ddlCity1, ddlCity1.SelectedItem.Value);
                if (!string.IsNullOrEmpty(txtZipCode.Text))
                    txtZipCode_New.Text = txtZipCode.Text;
            }
        }
    }
}
